<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./vue.js"></script>
</head>

<body>

  <div id="app">
    <h1>
      <input type="checkbox" v-model="isAllCheck" />全选
    </h1>

    <ul>
      <li v-for=" item in list" :key="item.id">
        <input type="checkbox" v-model="item.checked" />{{item.name}}--{{item.price+'元'}}
      </li>
    </ul>
    <p>{{index}}</p>
  </div>

</body>
<script>
  const { createApp, ref, computed } = Vue;
  createApp({
    setup() {

      const list = ref([
        { id: 1, name: '西瓜', price: 8, checked: false },
        { id: 2, name: '牛肉干', price: 28, checked: false },
        { id: 3, name: '猕猴桃', price: 15, checked: false }
      ])
      const isAllCheck = computed({
        get() {
          return list.value.every(v => v.checked);
        },
        set(val) {
          list.value.forEach(item => {
            item.checked = val;
          })
        }
      })

      const index = computed(() => {
        let total = 0;
        list.value.forEach(item => {
          if (item.checked) {
            total += item.price
          }
        })
        return total;
      })
      return {
        list, isAllCheck, index
      }
    }

  }).mount('#app')
</script>

</html>